// 封装购物车模块
import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { useUserStore } from "@/stores/user";
import { insertCartAPI,findNewCartListAPI,deleteCartAPI } from "@/apis/cart";
export const useCartStore = defineStore('cart', () => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    // 定义state - cartList
    const cartList = ref([])
    // 获取最新购物车数据列表action
    const getNewCartList = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.result
    }
    // 定义action - addCart
    const addCart = async (goods) => {
        const { skuId, count } = goods
        // 判断是否登录
        if (isLogin.value) {
            // 已登入 添加购物车操作
            await insertCartAPI({ skuId, count })
            getNewCartList()
        } else {
            // 未登录 添加本地购物车操作
            const item = cartList.value.find((item) => item.skuId === goods.skuId)
            if (item) {
                // 数量累加
                item.count++
            } else {
                // 没找到
                cartList.value.push(goods)
            }
        }


    }
    // 定义action - delCart
    const delCart = async (skuId) => {
        // 判断是否登录
        if (isLogin.value) {
            // 已登入 删除购物车操作
            await deleteCartAPI([skuId])
            getNewCartList()
        } else {
            // 未登入 删除本地购物车操作 删除指定skuId
            const index = cartList.value.findIndex((item) => item.skuId === skuId)
            cartList.value.splice(index, 1)
        }
    }
    // 退出后清空本地购物车数据
    const clearCart = () => {
        cartList.value = []
    }
    // 单选框功能
    const singleCheck = (skuId, selected) => {
        // 获取指定skuId的商品
        const item = cartList.value.find((item) => item.skuId === skuId)
        // 修改选中状态
        item.selected = selected
    }
    // 已选择商品数量
    const selectedCount = computed(() => cartList.value.filter((item) => item.selected).reduce((total, item) => total + item.count, 0))
    // 已选择商品总价
    const selectedPrice = computed(() => cartList.value.filter((item) => item.selected).reduce((total, item) => total + (item.count * item.price), 0))
    // 全选功能
    const allCheck = (selected) => {
        // 遍历购物车商品，修改选中状态
        cartList.value.forEach((item) => {
            item.selected = selected
        })
    }
    // 计算属性 - allCount - 计算购物车商品数量
    const allCount = computed(() => cartList.value.reduce((total, item) => total + item.count, 0))
    // 计算属性 - allPrice - 计算购物车商品总价
    const allPrice = computed(() => cartList.value.reduce((total, item) => total + (item.count * item.price), 0))
    // 计算属性 - allSelected - 计算购物车商品是否全选
    const allSelected = computed(() => cartList.value.every((item) => item.selected))
    // 返回数据
    return {
        cartList,
        allCount,
        allPrice,
        allSelected,
        selectedCount,
        selectedPrice,
        clearCart,
        addCart,
        delCart,
        singleCheck,
        allCheck,
        getNewCartList
    }
}, {
    // 自动持久化store数据到浏览器本地缓存
    persist: true
})